package com.tianyi.exception;

import lombok.Data;

/**
 * @Author tianyi
 * @Description // todo
 * @Date 2022 - 11 - 05
 **/
@Data
public class WebResponse<T> {
    private int code;
    private String message;
    private T data;

    public WebResponse() {
    }

    ;

    public WebResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public WebResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /**
     * 成功
     */
    public static <T> WebResponse<T> success(T data) {
        WebResponse<T> response = new WebResponse<>();
        response.setCode(RestMsgEnum.SUCCESS.getCode());
        response.setMessage(RestMsgEnum.SUCCESS.getMessage());
        response.setData(data);
        return response;
    }

    public static <T> WebResponse<T> fail(int code, String message, T data) {
        return new WebResponse<>(code, message, data);
    }

    /**
     * 失败
     */
    public static <T> WebResponse<T> fail(int code, String message) {
        return new WebResponse<>(code, message);
    }

    public static <T> WebResponse<T> fail(int code) {
        return new WebResponse<>(code, RestMsgEnum.FAIL.getMessage());
    }
}
